package org.example.window;

import org.apache.flink.api.common.eventtime.TimestampAssigner;
import org.apache.flink.api.common.eventtime.TimestampAssignerSupplier;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

import java.util.Arrays;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/2/23 09:36
 */
public class WindowInnerJoin {

    public static void main(String[] args) throws Exception{

        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        environment.setParallelism(1);

        WatermarkStrategy<Tuple2<String, Integer>> watermarkStrategy1 = WatermarkStrategy.<Tuple2<String, Integer>>forMonotonousTimestamps()
                .withTimestampAssigner((TimestampAssignerSupplier<Tuple2<String, Integer>>) context -> (element, recordTimestamp) -> element.f1 * 1000L);

        SingleOutputStreamOperator<Tuple2<String, Integer>> ds1 = environment.fromCollection(
                Arrays.asList(
                        Tuple2.<String, Integer>of("a", 1),
                        Tuple2.<String, Integer>of("a", 2),
                        Tuple2.<String, Integer>of("b", 10),
                        Tuple2.<String, Integer>of("b", 15)
                )
        ).assignTimestampsAndWatermarks(watermarkStrategy1);

        WatermarkStrategy<Tuple3<String, Integer,Integer>> watermarkStrategy2 = WatermarkStrategy.<Tuple3<String, Integer,Integer>>forMonotonousTimestamps()
                .withTimestampAssigner((TimestampAssignerSupplier<Tuple3<String, Integer,Integer>>) context -> (element, recordTimestamp) -> element.f1 * 1000L);

        SingleOutputStreamOperator<Tuple3<String, Integer, Integer>> ds2 = environment.fromCollection(
                Arrays.asList(
                        Tuple3.<String, Integer, Integer>of("a", 1, 2),
                        Tuple3.<String, Integer, Integer>of("c", 2, 3),
                        Tuple3.<String, Integer, Integer>of("b", 6, 4),
                        Tuple3.<String, Integer, Integer>of("b", 15, 5)
                )
        ).assignTimestampsAndWatermarks(watermarkStrategy2);

        DataStream<String> dataStream = ds1.join(ds2)
                .where(value -> value.f0)
                .equalTo(value -> value.f0)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .apply(new JoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {
                    @Override
                    public String join(Tuple2<String, Integer> first, Tuple3<String, Integer, Integer> second) throws Exception {
                        return first + "--->" + second;
                    }
                });
        dataStream.print();

        environment.execute();

    }

}
